<?php 
class Page{
    public $db;
    public $count; //记录数
    public $pageSize;
    public $totalPage;
    public $page;
    function __construct($db,$pageSize){  // 1
        $this->db = $db; // $this->db   DB类的对象
        $this->pageSize = $pageSize;
    }
    function getCount($sql){ //得到 sql 的记录数  2
        $this->count= $this->db->numRows($sql);   
    }
    function getTotalPage(){ //总页数  3
        $this->totalPage = ceil($this->count/$this->pageSize);
        
    }
    function main($sql){
        //$_GET 是一个全局变量  在类内部也是可以 直接获取 get传值
        if($_GET["page"]){ //4 
            $this->page = $_GET["page"];
            if($this->page>=$this->totalPage){
                $this->page = $this->totalPage;
            }
        }else{
            $this->page = 1;
        }
        //  5
        $start = ($this->page-1) * $this->pageSize;
        $sql = $sql." limit ".$start.",".$this->pageSize;
        $rs = $this->db->fetchAll($sql);
        return $rs;        
    }
    function show(){ //分页器
           $str=" 共".$this->totalPage."页/当前第".$this->page."页  共".$this->count."条";
           $str.="   | ";
           $str.="     <a href='?page=1'>首页</a>";
            $str.="    |";
           $str.="     <a href='?page=".($this->page-1)."'>上一页</a>";
            $str.="    |";
            
        if($this->page<5){
			for($i=1;$i<=10;$i++){
				if($i==$this->totalPage){
		
				$str.="	<a href='?page=".$i."'>[".$i."]</a>";
			
					break;
				}
		
		$str.="	<a href='?page=".$i."'>[".$i."]</a>";
				
			}
		}else{
			for($i=($this->page-4);$i<=($this->page+5);$i++){
				if($i==$this->totalPage){
		           $str.="	<a href='?page=".$i."'>[".$i."]</a>";	
					break;
				}
		        $str.="	<a href='?page=".$i."'>[".$i."]</a>";		
			}
		}
             $str.="    |";
            $str.="    <a href='?page=".($this->page+1)."'>下一页</a>";
             $str.="   |";
            $str.="    <a href='?page=".$this->totalPage."'>尾页</a>";
            echo $str;
    }
}
?>